1. 适配器模式 (Adapter Pattern)
适配器模式的Java实现

// 目标接口
interface Target {
    void request();
}

// 需要被适配的类
class Adaptee {
    public void specificRequest() {
        System.out.println("Adaptee: Specific request");
    }
}

// 适配器类
class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        // 使用适配者的方法来满足目标接口的需求
        adaptee.specificRequest();
    }
}

// 客户端
public class Client {
    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Target target = new Adapter(adaptee);
        target.request();  // 输出: Adaptee: Specific request
    }
}
2. 装饰器模式 (Decorator Pattern)
装饰器模式的Java实现

// 抽象组件
interface Component {
    void operation();
}

// 具体组件
class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("ConcreteComponent: Operation");
    }
}

// 装饰器
abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    public void operation() {
        component.operation();
    }
}

// 具体装饰器A
class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        addedBehavior();  // 添加额外的行为
    }

    private void addedBehavior() {
        System.out.println("ConcreteDecoratorA: Added behavior");
    }
}

// 客户端
public class Client {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        Component decoratedComponent = new ConcreteDecoratorA(component);
        decoratedComponent.operation();  // 输出: ConcreteComponent: Operation, ConcreteDecoratorA: Added behavior
    }
}
3. 桥接模式 (Bridge Pattern)
桥接模式的Java实现

// 实现化角色
interface Implementor {
    void operationImpl();
}

// 具体实现化角色
class ConcreteImplementorA implements Implementor {
    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorA: Operation implementation");
    }
}

// 抽象化角色
abstract class Abstraction {
    protected Implementor implementor;

    public void setImplementor(Implementor implementor) {
        this.implementor = implementor;
    }

    public abstract void operation();
}

// 扩展抽象化角色
class RefinedAbstraction extends Abstraction {
    @Override
    public void operation() {
        implementor.operationImpl();
    }
}

// 客户端
public class Client {
    public static void main(String[] args) {
        Implementor implementor = new ConcreteImplementorA();
        Abstraction abstraction = new RefinedAbstraction();
        abstraction.setImplementor(implementor);
        abstraction.operation();  // 输出: ConcreteImplementorA: Operation implementation
    }
}
4. 组合模式 (Composite Pattern)
组合模式的Java实现

// 组件接口
interface Component {
    void operation();
}

// 叶子节点
class Leaf implements Component {
    @Override
    public void operation() {
        System.out.println("Leaf: Operation");
    }
}

// 复合节点
class Composite implements Component {
    private List<Component> children = new ArrayList<>();

    public void add(Component component) {
        children.add(component);
    }

    public void remove(Component component) {
        children.remove(component);
    }

    @Override
    public void operation() {
        for (Component child : children) {
            child.operation();
        }
    }
}

// 客户端
public class Client {
    public static void main(String[] args) {
        Component leaf1 = new Leaf();
        Component leaf2 = new Leaf();
        Component composite = new Composite();
        composite.add(leaf1);
        composite.add(leaf2);
        composite.operation();  // 输出: Leaf: Operation, Leaf: Operation
    }
}